package com.hnust.controller;

import com.hnust.domain.College;
import com.hnust.domain.record.LoginData;
import com.hnust.service.TeacherService;
import com.hnust.util.JwtToken;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人信息管理板块接口
 * @author 小游
 * @date 2020/10/29
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 登录，教工号和密码
    @PostMapping("/login")
    public Result<LoginData> Login (
            @RequestParam("faculty_number") String faculty_number,
            @RequestParam("password") String password
    ){
        try {
            LoginData loginData=teacherService.login(faculty_number, password);
            if(loginData!=null){
                return new Result<>(1,"登录成功",loginData);
            }
            return new Result<>(0,"登录失败",null);
        } catch (Exception e){
            return new Result<>(0,"登录出现异常",null);
        }
    }

    // 登录，教工号和密码
    @PostMapping("/register")
    public Result<Boolean> register (
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "number", required = true) String number,
            @RequestParam(value = "password", required = true) String password,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "collegeId", required = true) String collegeId
    ){
        String msg=teacherService.register(name,number, password,phone,email,collegeId);
        switch (msg) {
            case "注册成功":
                return new Result<>(1, msg, true);
            case "注册失败，该工号已经被注册了":
            case "注册失败，该手机号已经被注册了":
                return new Result<>(1, msg, false);
            case "注册出现异常":
                return new Result<>(0, msg, false);
            default:
                return new Result<>(0, "注册出现异常", false);
        }
    }
    @RequestMapping("/getAllCollege")
    public Result<List<College>> getAllCollege(){
        return new Result<>(1,"获取所有学院成功",teacherService.getAllCollege());
    }
}
